/**
 * @Title: BaseController.java
 * @Package com.lemon.controller
 * @Description: (一句话阐述):
 * @author Ran_cc
 * @date 2018年5月30日
 * @version V1.0
 */
package com.lemon.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lemon.conf.MyException;
import com.lemon.model.cityRun.CrShopUser;
import com.lemon.model.cityRun.CrSysLogs;
import com.lemon.service.cityRun.CrShopUserService;
import com.lemon.utils.IpUtils;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author Ran_cc
 * @date 2018年5月30日
 * @版本 V 1.0
 */
@RestController
public class BaseController {

	@Resource
	private CrShopUserService uService;

	public static Logger log = LoggerFactory.getLogger(BaseController.class);

	public CrShopUser getCrShopUser(HttpServletRequest request) {

		return (CrShopUser) request.getSession().getAttribute("user");
	}

	@Resource
	private CrShopUserService userService;

	@RequestMapping("/qtq")
	public void index(HttpServletRequest request , HttpServletResponse response) throws IOException , ServletException {
		log.info("重定向到首页");
		response.setStatus(302);
		response.setHeader("location" , request.getContextPath() + "/adm/index.html");
		//		RequestDispatcher view = request.getRequestDispatcher("/adm/index.html");
		//		view.forward(request , response);
	}

	@RequestMapping("/index2")
	public void index2(HttpServletRequest request , HttpServletResponse response)
			throws IOException , ServletException {
		log.info("重定向到首页");
		response.setStatus(302);
		response.setHeader("location" , request.getContextPath() + "/adm/index.html");
		//		RequestDispatcher view = request.getRequestDispatcher("/adm/index.html");
		//		view.forward(request , response);
	}

	@RequestMapping("/")
	public void index3(HttpServletRequest request , HttpServletResponse response)
			throws IOException , ServletException {
		log.info("重定向到首页");
		response.setStatus(302);
		StringBuffer url = request.getRequestURL();
		String tempContextUrl = url.delete(url.length() - request.getRequestURI().length() , url.length())
				.append(request.getServletContext().getContextPath()).append("/").toString();
		log.info("域名:端口:项目名---" + tempContextUrl);
		response.setHeader("location" , tempContextUrl + "adm/index.html");
		//		RequestDispatcher view = request.getRequestDispatcher("/adm/index.html");
		//		view.forward(request , response);
	}

	//添加操作日志
	public void addLogs(String handle , HttpServletRequest request , HttpServletResponse response) {
		CrShopUser u = (CrShopUser) request.getSession().getAttribute("user");
		if (u.getUserType() != 1) {
			return;
		}
		try {
			CrSysLogs logs = new CrSysLogs();
			logs.setAccount(u.getUserAccount());
			logs.setGroupId(u.getRuleId());
			logs.setGroupName(u.getRuleName());
			logs.setHandle(handle);
			logs.setIp(IpUtils.getIpAddr(request));
			logs.setStatus(1);
			uService.addLogsBySelective(logs);
		} catch (Exception e) {
			MyException.writeErr(e , "添加操作日志异常");
		}
	}

}
